package com.my.intercepter;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.my.bean.User;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;

public class MethodIntercepter extends MethodFilterInterceptor {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected String doIntercept(ActionInvocation invocation) throws Exception {

		System.out.println("-----调用了自定义拦截器了... 调用Action前 ...");
		Map<String, Object> session = invocation.getInvocationContext()
				.getSession();
		HttpServletRequest request = ServletActionContext.getRequest();
		User u = (User) session.get("user");
		String result = "";
		if (u == null) {
			request.setAttribute("message",
					"<script laguage='JavaScript'> alert('您尚未登录!') </script>");
			return "login";
		}
		result = invocation.invoke();
		System.out.println("-----调用了自定义拦截器了... 调用Action后 ...");
		return result;
	}
}
